import React from 'react';
import { Table, Button } from 'antd';
import CodeLabel from '../../../components/CodeLabel';
import Modal from '../../../components/Modal';
import PolicyDetail from '../../Case/Check/Detail/policyDetail';
import PolicyFlow from '../../Case/Check/Detail/policyFlow';
import PolicyModal from '../../Case/Check/Detail/policyModal';
import { STRATEGY_TYPE } from '../../../utils/constant';
import Result from '../../../components/Result';

export default function expandedList(props) {
  // 获取列
  function getColumn() {
    return [
      {
        title: '序号',
        key: 'index',
        width: 80,
        render: (text, record, index) => {
          return <>{index + 1}</>;
        }
      },
      {
        title: '策略名称',
        key: 'strategy_name',
        dataIndex: 'strategy_name',
        render: (text, record) => {
          return (
            <Button type='link' onClick={showPolicy.bind(this, record)}>
              {text}
            </Button>
          );
        }
      },
      {
        title: '策略类型',
        key: 'strategy_type_name',
        dataIndex: 'strategy_type_name'
      },
      {
        title: '策略模式',
        key: 'strategy_mode_name',
        dataIndex: 'strategy_mode_name'
      },
      {
        title: '优先级',
        key: 'prior',
        dataIndex: 'prior'
      },
      {
        title: '执行结果',
        key: 'hit_result',
        dataIndex: 'hit_result',
        render: text => {
          return (
            <>
              {text ? (
                <Result value={text}>
                  <CodeLabel code='hit_handle' value={text} />
                </Result>
              ) : (
                text
              )}
            </>
          );
        }
      },
      {
        title: '备注',
        key: 'comments',
        dataIndex: 'comments'
      },
      {
        title: '操作',
        key: 'operate',
        render: (text, record) => {
          return (
            <Button type='link' onClick={showPolicyDetail.bind(this, record)}>
              命中详情
            </Button>
          );
        }
      }
    ];
  }

  function showPolicyDetail(record) {
    const Component = record.strategy_type === STRATEGY_TYPE._COMMON ? PolicyDetail : PolicyFlow;
    Modal.open({
      title: '命中详情',
      width: '1000px',
      content: <Component record={record} {...props} onlyShowHitDetail={true} />,
      isOkBtn: true,
      maskClosable: false,
      isCancelBtn: true,
      ok: () => {
        Modal.close();
      }
    });
  }

  function showPolicy(record) {
    Modal.open({
      title: '策略详情',
      width: '1200px',
      content: <PolicyModal record={record} {...props} actionType={'detail'} model={false} />,
      isOkBtn: true,
      maskClosable: false,
      isCancelBtn: true,
      ok: () => {
        Modal.close();
      }
    });
  }
  return (
    <div style={{ padding: '10px 20px 20px 20px' }}>
      <Table columns={getColumn()} dataSource={props.data} pagination={false} rowKey='strategy_name' />
    </div>
  );
}
